package it.irondev.appturismo.action;

import it.irondev.appturismo.entity.City;
import it.irondev.appturismo.entity.Gallery;
import it.irondev.appturismo.entity.Language;
import it.irondev.appturismo.entity.Restaurant;
import it.irondev.appturismo.entity.Restauranttype;
import it.irondev.appturismo.filter.BaseFilter;
import it.irondev.appturismo.filter.RestaurantFilter;
import it.irondev.appturismo.service.ICityService;
import it.irondev.appturismo.service.IGalleryService;
import it.irondev.appturismo.service.ILanguageService;
import it.irondev.appturismo.service.IRestaurantService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller("restaurantAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class RestaurantAction extends AbstractAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7058774121130781168L;

	@Autowired
	IRestaurantService serviceRestaurant;

	@Autowired
	ILanguageService serviceLanguage;

	@Autowired
	ICityService serviceCity;

	@Autowired
	IGalleryService galleryService;

	private Integer idGallery;
	
	private Long idRestaurant;
	private List<Language> listLanguage;
	private List<Restauranttype> listType;
	private List<City> listCity;
	private Restaurant entityBean;
	private List<Restaurant> restaurants;
	private String type;
	
	private String stringaSearchLanguage;


	public String list() {
		listLanguage = serviceLanguage.getAllLanguages();
		setStringaSearchLanguage(buildStringSearch(listLanguage)); 
		return "listRestaurant";
	}
	
	public String listJson() {
		restaurants = serviceRestaurant.getAllRestaurants();
		return "listRestaurantJson";
	}
	

	public String nuovo() {
		listType = serviceRestaurant.getAllRestaurantTypes();
		listCity = serviceCity.getAllCity();
		listLanguage = serviceLanguage.getAllLanguages();
		return "newRestaurant";
	}

	
	
	public String edit() {
		listLanguage = serviceLanguage.getAllLanguages();
		listType = serviceRestaurant.getAllRestaurantTypes();
		listCity = serviceCity.getAllCity();
		entityBean = serviceRestaurant.getRestaurantById(idRestaurant.intValue());
		return "editRestaurant";
	}
	
	
	private String buildStringSearch(List<Language> listLanguage2) {
		String stringa = "{value:'";
		for (Language language : listLanguage2) {
			stringa+=language.getName()+":"+language.getName()+";";
		}
		stringa+="'}";
		return stringa;
	}

	public void delete() {
		if (getOper().equalsIgnoreCase("del")) {
			int idEntity = Integer.parseInt(getId());
			serviceRestaurant.deleteRestaurant(idEntity);
		}
	}

	public String update() {
		serviceRestaurant.updateRestaurant(entityBean);
		return "listRestaurant";
	}
	
	
	public String add() {
		serviceRestaurant.addRestaurant(entityBean);
		return "listRestaurant";
	}

	
	public String addGallery() {
		Gallery tmpGallery = new Gallery();
		galleryService.addGallery(tmpGallery);
		idGallery = tmpGallery.getIdgallery();
		entityBean = serviceRestaurant.getRestaurantById(idRestaurant.intValue());
		entityBean.setGallery(galleryService.getGalleryById(idGallery));
		serviceRestaurant.updateRestaurant(entityBean);
		return "addRestaurantGallery";
	}
	
	

	/*
	 * MOBILE ACTION
	 * 
	 */
	
	
	public String listMobile() {
		RestaurantFilter filter= new RestaurantFilter();
		filter.setIdlanguage(serviceLanguage.getLanguageByPrefix(ActionContext.getContext().getLocale().getLanguage()).getIdlanguage());
		filter.setPremium(true);
		if(type!=null&&!type.isEmpty())
			filter.setType(serviceRestaurant.getRestaurantTypeBySigla(type).getIdrestauranttype());
		filter.setFavouriteCity(getFavouriteCity());
		setRestaurants(serviceRestaurant.getRestaurantsByFilter(filter));
		return "listRestaurantMobile";
	}

	public String detailMobile() {
		entityBean = serviceRestaurant.getRestaurantById(idRestaurant.intValue());
		return "detailRestaurantMobile";
	}
	


	
	
	public Long getIdRestaurant() {
		return idRestaurant;
	}



	public void setIdRestaurant(Long idRestaurant) {
		this.idRestaurant = idRestaurant;
	}



	public List<City> getListCity() {
		return listCity;
	}


	public void setListCity(List<City> listCity) {
		this.listCity = listCity;
	}


	public Restaurant getEntityBean() {
		return entityBean;
	}


	public void setEntityBean(Restaurant entityBean) {
		this.entityBean = entityBean;
	}


	public List<Restaurant> getRestaurants() {
		return restaurants;
	}


	public void setRestaurants(List<Restaurant> restaurants) {
		this.restaurants = restaurants;
	}

	public List<Language> getListLanguage() {
		return listLanguage;
	}


	public void setListLanguage(List<Language> listLanguage) {
		this.listLanguage = listLanguage;
	}


	public List<Restauranttype> getListType() {
		return listType;
	}


	public void setListType(List<Restauranttype> listType) {
		this.listType = listType;
	}


	public String getStringaSearchLanguage() {
		return stringaSearchLanguage;
	}

	public void setStringaSearchLanguage(String stringaSearchLanguage) {
		this.stringaSearchLanguage = stringaSearchLanguage;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	
	/**
	 * @return the idGallery
	 */
	public Integer getIdGallery() {
		return idGallery;
	}

	/**
	 * @param idGallery the idGallery to set
	 */
	public void setIdGallery(Integer idGallery) {
		this.idGallery = idGallery;
	}



}
